{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 09_Train_final_model_TransE_l1\n",
    "#\n",
    "# created by LuYF-Lemon-love <luyanfeng_nlp@qq.com> on March 11, 2023\n",
    "# updated by LuYF-Lemon-love <luyanfeng_nlp@qq.com> on March 11, 2023\n",
    "#\n",
    "# 该脚本展示了如何在 DRKG 上训练最终模型 (TransE_l1).\n",
    "#\n",
    "# 需要的包:\n",
    "#          torch\n",
    "#          dgl, version: 0.4.3\n",
    "#          dglke\n",
    "#\n",
    "# 需要的文件:\n",
    "#          ../../data/drkg/drkg.tsv\n",
    "#\n",
    "# 源教程链接: https://github.com/gnn4dr/DRKG/blob/master/embedding_analysis/Train_embeddings.ipynb"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Training DRKG Using TransE_l1\n",
    "\n",
    "这个 notebook 展示了如何在 DRKG 上训练最终模型 (TransE_l1)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们能使用 DGL-KE 命令训练 TransE_l1 模型, 关于如何使用 DGL-KE 的更多信息请参考 https://github.com/awslabs/dgl-ke.\n",
    "\n",
    "这里我们使用两个 GPU 训练模型."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- batch_size: **4096**\n",
    "\n",
    "- neg_sample_size: **256**\n",
    "\n",
    "- hidden_dim: 200, **400**\n",
    "\n",
    "- gamma: 6, 12, **18**\n",
    "\n",
    "- lr: 0.01, **0.05**, 0.1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Reading train triples....\n",
      "Finished. Read 5874261 train triples.\n",
      "|Train|: 5874261\n",
      "random partition 5874261 edges into 2 parts\n",
      "part 0 has 2937131 edges\n",
      "part 1 has 2937130 edges\n",
      "/home/luyanfeng/miniconda3/envs/drkg/lib/python3.8/site-packages/dgl/base.py:25: UserWarning: multigraph will be deprecated.DGL will treat all graphs as multigraph in the future.\n",
      "  warnings.warn(msg, warn_type)\n",
      "Total initialize time 14.936 seconds\n",
      "[proc 1][Train](20000/100000) average pos_loss: 0.5498470231160522\n",
      "[proc 1][Train](20000/100000) average neg_loss: 0.5312641047999961\n",
      "[proc 0][Train](20000/100000) average pos_loss: 0.5582810022801161\n",
      "[proc 1][Train](20000/100000) average loss: 0.5405555639073253\n",
      "[proc 0][Train](20000/100000) average neg_loss: 0.5313805567718548\n",
      "[proc 1][Train](20000/100000) average regularization: 0.00013342677711552825\n",
      "[proc 1][Train] 20000 steps take 1076.122 seconds\n",
      "[proc 1]sample: 64.620, forward: 426.971, backward: 52.664, update: 530.470\n",
      "[proc 0][Train](20000/100000) average loss: 0.5448307793557644\n",
      "[proc 0][Train](20000/100000) average regularization: 0.0001346286157342547\n",
      "[proc 0][Train] 20000 steps take 1076.151 seconds\n",
      "[proc 0]sample: 65.337, forward: 414.275, backward: 51.393, update: 524.151\n",
      "[proc 1][Train](40000/100000) average pos_loss: 0.3955300693139434\n",
      "[proc 0][Train](40000/100000) average pos_loss: 0.39577988470941783\n",
      "[proc 0][Train](40000/100000) average neg_loss: 0.49664788482785227\n",
      "[proc 1][Train](40000/100000) average neg_loss: 0.4962424231305718\n",
      "[proc 0][Train](40000/100000) average loss: 0.44621388473063706\n",
      "[proc 1][Train](40000/100000) average loss: 0.4458862462222576\n",
      "[proc 0][Train](40000/100000) average regularization: 0.00011444124439294682\n",
      "[proc 1][Train](40000/100000) average regularization: 0.00011446012051528669\n",
      "[proc 0][Train] 20000 steps take 1064.101 seconds\n",
      "[proc 0]sample: 64.126, forward: 408.538, backward: 50.762, update: 520.128\n",
      "[proc 1][Train] 20000 steps take 1064.110 seconds\n",
      "[proc 1]sample: 64.275, forward: 422.725, backward: 51.615, update: 524.614\n",
      "[proc 1][Train](60000/100000) average pos_loss: 0.39127242613732816\n",
      "[proc 0][Train](60000/100000) average pos_loss: 0.3915310000255704\n",
      "[proc 1][Train](60000/100000) average neg_loss: 0.49070250047147274\n",
      "[proc 1][Train](60000/100000) average loss: 0.4409874633848667\n",
      "[proc 0][Train](60000/100000) average neg_loss: 0.4906350355684757\n",
      "[proc 1][Train](60000/100000) average regularization: 0.00011795765341594233\n",
      "[proc 0][Train](60000/100000) average loss: 0.4410830178469419\n",
      "[proc 1][Train] 20000 steps take 1080.599 seconds\n",
      "[proc 1]sample: 64.259, forward: 429.118, backward: 53.849, update: 532.957\n",
      "[proc 0][Train](60000/100000) average regularization: 0.00011793401769646152\n",
      "[proc 0][Train] 20000 steps take 1080.608 seconds\n",
      "[proc 0]sample: 65.062, forward: 416.431, backward: 51.814, update: 526.363\n",
      "[proc 1][Train](80000/100000) average pos_loss: 0.38924458851218224\n",
      "[proc 0][Train](80000/100000) average pos_loss: 0.3895254679888487\n",
      "[proc 1][Train](80000/100000) average neg_loss: 0.487964294500649\n",
      "[proc 0][Train](80000/100000) average neg_loss: 0.487627767893672\n",
      "[proc 1][Train](80000/100000) average loss: 0.43860444138348104\n",
      "[proc 0][Train](80000/100000) average loss: 0.43857661796063185\n",
      "[proc 1][Train](80000/100000) average regularization: 0.00012009999070432968\n",
      "[proc 1][Train] 20000 steps take 1074.056 seconds\n",
      "[proc 1]sample: 65.183, forward: 426.921, backward: 52.416, update: 528.569\n",
      "[proc 0][Train](80000/100000) average regularization: 0.00012008158780008671\n",
      "[proc 0][Train] 20000 steps take 1074.052 seconds\n",
      "[proc 0]sample: 64.198, forward: 409.239, backward: 50.654, update: 521.178\n",
      "[proc 1][Train](100000/100000) average pos_loss: 0.38791376684755086\n",
      "[proc 0][Train](100000/100000) average pos_loss: 0.38820766239613297\n",
      "[proc 1][Train](100000/100000) average neg_loss: 0.4861085918486118\n",
      "[proc 1][Train](100000/100000) average loss: 0.4370111793294549\n",
      "[proc 0][Train](100000/100000) average neg_loss: 0.485720903994143\n",
      "[proc 1][Train](100000/100000) average regularization: 0.00012161216688000423\n",
      "[proc 1][Train] 20000 steps take 1080.298 seconds\n",
      "[proc 1]sample: 64.804, forward: 430.681, backward: 53.562, update: 530.988\n",
      "proc 1 takes 5375.186 seconds\n",
      "[proc 0][Train](100000/100000) average loss: 0.43696428323090075\n",
      "[proc 0][Train](100000/100000) average regularization: 0.00012159616475801159\n",
      "[proc 0][Train] 20000 steps take 1080.299 seconds\n",
      "[proc 0]sample: 64.324, forward: 407.874, backward: 50.743, update: 521.006\n",
      "proc 0 takes 5375.213 seconds\n",
      "training takes 5375.640636205673 seconds\n",
      "Save model to ckpts/TransE_l1_All_DRKG_0\n"
     ]
    }
   ],
   "source": [
    "!DGLBACKEND=pytorch dglke_train --dataset All_DRKG --data_path ../../data/drkg \\\n",
    "--data_files drkg.tsv --format 'raw_udd_hrt' \\\n",
    "--model_name TransE_l1 \\\n",
    "--batch_size 4096 --neg_sample_size 256 --hidden_dim 400 \\\n",
    "--gamma 18.0 --lr 0.05 --max_step 100000 -adv --regularization_coef 1.00E-07 \\\n",
    "--gpu 0 1 --num_proc 2 --mix_cpu_gpu --async_update --force_sync_interval 1000 \\\n",
    "--log_interval 20000 --num_thread 32"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.16"
  },
  "vscode": {
   "interpreter": {
    "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
